#include "stm32f10x.h"                  // Device header

u16 TransferTime;

void DMAConfig_Init(u32 addrA,u32 addrB,u16 transfer_time)
{
	TransferTime = transfer_time;
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
	DMA_InitTypeDef DMA_InitStructure;
	DMA_InitStructure.DMA_PeripheralBaseAddr = addrA;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//Byte,Half_Word(16bit),Word(32bit)
	DMA_InitStructure.DMA_PeripheralInc  = DMA_PeripheralInc_Enable;//自增
	DMA_InitStructure.DMA_MemoryBaseAddr = addrB;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
	DMA_InitStructure.DMA_MemoryInc  = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//传输方向：外设作为传输起点
	DMA_InitStructure.DMA_BufferSize = transfer_time;//传输次数
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//还有Circular为自动重装模式
	DMA_InitStructure.DMA_M2M = DMA_M2M_Enable;//内存到内存（实则为开启软件触）
	DMA_InitStructure.DMA_Priority =  DMA_Priority_Medium;//有多个通道时可以控制通道优先级
	DMA_Init(DMA1_Channel1,&DMA_InitStructure);
	
	DMA_Cmd(DMA1_Channel1,DISABLE);
}

void DMAConfig_SetTransferTime(u16 time)
{
	TransferTime = time;
	DMA_Cmd(DMA1_Channel1,DISABLE);
	DMA_SetCurrDataCounter(DMA1_Channel1,time);
	DMA_Cmd(DMA1_Channel1,ENABLE);
}

void DMAConfig_Transfer(void)
{
	DMAConfig_SetTransferTime(TransferTime);
	while(DMA_GetFlagStatus(DMA1_FLAG_TC1) == RESET);//等待传输完成
	DMA_ClearFlag(DMA1_FLAG_TC1);
}


